package com.sugon.controller.sys;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.FactoryTypeEntity;
import com.sugon.handler.EasyPoiExcelDictHandlerImpl;
import com.sugon.service.FactoryTypeService;
import com.sugon.service.SysConfigService;
import com.sugon.utils.MapUtil;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

/**
 * 设备厂家Controller
 *
 * @author sugon
 * @date 2020-03-06 10:13:55
 */
@Controller
@RequestMapping("factorytype")
public class FactoryTypeController {
    @Resource
    private FactoryTypeService factoryTypeService;
    @Resource
    private SysConfigService configService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("factorytype:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<FactoryTypeEntity> factoryTypeList = factoryTypeService.queryList(query);
        int total = factoryTypeService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(factoryTypeList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        FactoryTypeEntity factoryType = factoryTypeService.queryObject(id);

        return R.ok().put("factoryType", factoryType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("factorytype:save")
    @ResponseBody
    public R save(@RequestBody FactoryTypeEntity factoryType) {
        factoryTypeService.save(factoryType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("factorytype:update")
    @ResponseBody
    public R update(@RequestBody FactoryTypeEntity factoryType) {
        factoryTypeService.update(factoryType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("factorytype:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        factoryTypeService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<FactoryTypeEntity> list = factoryTypeService.queryList(params);
        return R.ok().put("list", list);
    }

    @RequestMapping("/imports")
    @ResponseBody
    public R imports(@RequestParam("file") MultipartFile file) throws Exception {
        return R.ok("成功导入" + factoryTypeService.imports(file) + "条厂商记录");
    }

    @RequestMapping("/export")
    @ResponseBody
    public void export(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
        // 防止查询参数Map中出现空参数
        MapUtil.removeEmptyValue(params);
        modelMap.put(TemplateExcelConstants.FILE_NAME, "厂家类型_" + com.sugon.modules.utils.DateUtil.today());
        // 设置导出模板参数
        TemplateExportParams templateExportParams = new TemplateExportParams(configService.getPoiTemplatePath() + "factoryTypeIETemplate.xlsx");
        templateExportParams.setDictHandler(new EasyPoiExcelDictHandlerImpl());
        modelMap.put(TemplateExcelConstants.PARAMS, templateExportParams);
        // 设置模板参数
        modelMap.put(TemplateExcelConstants.MAP_DATA, factoryTypeService.prepareExportData(params));
        PoiBaseView.render(modelMap, request, response, TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
    }
}
